package com.dlwx.parttimetalent.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.IncomeHisAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.IcomeHisBean;
import com.dlwx.parttimetalent.bean.IncomeBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.GetAgeInter;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.IncomeFilterDialog;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class IncomeHistoryActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private IncomeHisAdapter mAdapter;
    private ArrayList<IncomeBean> mDatas;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_history);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void initView() {
        tvMiddleTitle.setText("收支记录");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleRightOneTv.setText("筛选");
        titleRightOneTv.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(IncomeHistoryActivity.this));
        mAdapter = new IncomeHisAdapter(IncomeHistoryActivity.this);
        mAdapter.setDataList(new ArrayList<IcomeHisBean.BodyBean>());
         lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(IncomeHistoryActivity.this)
                .setHeight(R.dimen.size_1)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.bg)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                mAdapter.clear();
                lRecyclerViewAdapter.notifyDataSetChanged();
                getDatas();
            }
        });
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }
        });
    }
    private String selectDate="";
    private String listStatus="";
    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.CapitalList,IncomeHistoryActivity.this)
                .params("selectDate",selectDate)
                .params("listStatus",listStatus)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        IcomeHisBean parentBean= DealResult.getInstance().dealDatas(IncomeHistoryActivity.this,response,new TypeToken<IcomeHisBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS||code==Constant.EMPTY){
                            setDatas(parentBean.getBody());
                        }else {
                            ToastUtils.toastForShort(IncomeHistoryActivity.this,parentBean.getResult());

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(IncomeHistoryActivity.this,getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(List<IcomeHisBean.BodyBean> body) {
        mAdapter.addAll(body);
        recyclerView.refreshComplete(mAdapter.getItemCount());
    }

    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_tv:
                IncomeFilterDialog dialog=new IncomeFilterDialog(this, new GetAgeInter() {
                    @Override
                    public void getAge(int year, int month, int day, String start) {
                        mAdapter.clear();
                        lRecyclerViewAdapter.notifyDataSetChanged();
                        if(year==0){
                            listStatus="";
                        }else{
                            listStatus=""+ year;
                        }
                        selectDate=start;
                        getDatas();
                    }
                });
                dialog.show();
                break;
        }
    }
}
